package exe3.sp;

//商场类
//名称，地址等信息
public class Market {
    String mallname;//名称
    String address;//地址
    Member member[] = new Member[100];

    int pos = 0;//当前会员卡人数

    public Market() {
    }

    public Market(String mallname, String address, Member[] member, int pos) {
        this.mallname = mallname;
        this.address = address;
        this.member = member;
        this.pos = pos;
    }

    //添加会员
    public boolean checkCardNoRepeat(Member memberid) {
        if (pos >= member.length) {
            System.out.println("会员已满");
        }
        for (int i = 0; i < pos; i++) {
            if (member[i].memberid.equals(memberid.memberid)) {
                System.out.println("禁止添加");
                return false;
            }
        }
        member[pos++] = memberid;
        return true;
    }

    public boolean checkCardNoRepeat(String name, String memberid, String phonenumber, double integral) {
        if (pos >= member.length) {
            System.out.println("会员已满");
            return false;
        }
        member[pos++] = new Member(name, memberid, phonenumber, integral);
        return true;
    }

    //给某一用户涨分的方法。
    public boolean inc(String memberid, double integral) {
        for (int i = 0; i < pos; i++) {
            if (member[i].memberid.equals(memberid)) {
                member[i].integral = member[i].integral + integral;
                System.out.println("积分增加成功,目前积分是:" + member[i].integral);
                return true;
            }
        }
        System.out.println("会员号不存在");
        return false;
    }

    //兑换礼品
    public String duihuan(String memberid, int choice) {
        for (int i = 0; i < pos; i++) {
            if (member[i].memberid.equals(memberid)) {
                switch ((choice)) {
                    case 1:
                        if (member[i].integral >= 100) {
                            member[i].integral -= 100;

                            return "餐巾纸一包";
                        }
                        return null;
                    case 2:
                        if (member[i].integral >= 400) {
                            member[i].integral -= 400;
                            return "剃须刀一把";
                        }
                        System.out.println("积分不足！无法兑换");
                        return null;
                    case 3:
                        if (member[i].integral >= 1000) {
                            member[i].integral -= 1000;
                            System.out.println("兑换炒锅一口成功!");
                            return "炒锅一口";
                        }

                        return null;
                    default:
                        System.out.println("输入错误!");
                        return null;
                }
            }
        }
        return null;
    }

    //查看会员信息
    public void show() {
        System.out.println("姓名\t会员号\t手机号\t积分");
        for (int i = 0; i < pos; i++) {
            System.out.print(i + 1 + "\t");
            member[i].show();
        }
    }

    //会员按照积分从高到低来排序
    public void bubbleSort(){
        Member temp;
        for(int i=0;i<pos-1;i++){
            for(int j=0;j<pos-1-i;j++){
                //j和j+1比较
                if(member[j].integral<member[j+1].integral){
                    temp=member[j];
                    member[j]=member[j+1];
                    member[j+1]=temp;
                }
            }
        }
    }
}
